package com.yy.blogdemo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.blogdemo.domain.entity.Article;
import com.yy.blogdemo.domain.vo.ArticleVo;
import com.yy.blogdemo.domain.vo.params.PageParams;
import com.yy.blogdemo.mapper.Articlemapper;
import com.yy.blogdemo.service.ArticleService;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private Articlemapper articlemapper;

    @Override
    public List<ArticleVo> listArticlesPage(PageParams pageParams) {
        //构造
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        //按照降序排列
        queryWrapper.orderByDesc(Article::getWeight,Article::getCreateDate);

        Page<Article> page = new Page<>(pageParams.getPage(),pageParams.getPagesize());

        Page<Article> articlePage =articlemapper.selectPage(page,queryWrapper);

        List<Article> articleList =articlePage.getRecords();

        List<ArticleVo>articleVoList =CopyList(articleList);

        return articleVoList;
    }
    private ArticleVo copy(Article article){
        ArticleVo articleVo =new ArticleVo();
        //beanutils拷贝相似工具
        BeanUtils.copyProperties(article,articleVo);

        articleVo.setCreateDate(new DateTime(article.getCreateDate()).toString("yyyy-MM-dd HH:mm:ss"));

        return articleVo;
    }

    private List<ArticleVo> CopyList(List<Article> articleList) {
        List<ArticleVo> list = new ArrayList<>();
        for (Article article:articleList){
            list.add(copy(article));
        }
        return list;
    }
}
